<?php
/**
 * Created by PhpStorm.
 * User: wdt
 * Date: 2016/3/18
 * Time: 18:02
 */

namespace front\modules\app\controllers;



use front\controllers\WebBaseController;
use goods\services\CartService;

class CartController extends WebBaseController{

    /**
     * 添加商品到购物车
     * @return array
     */
    public function actionAdd()
    {
        $data = \Yii::$app->request->post();
        if (empty($data['shop_id'])) {
            WebBaseController::getAjaxData(2,'未传递shop_id');
        }
        if (empty($data['uid'])) {
            WebBaseController::getAjaxData(3,'未传递uid');
        }
        if (empty($data['product_id'])) {
            WebBaseController::getAjaxData(4,'未传递product_id');
        }
        if (empty($data['num'])) {
            WebBaseController::getAjaxData(5,'未传递num');
        }
        $ret = CartService::addCart($data);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,'添加商品到购物车成功');
        }
        WebBaseController::getAjaxData(6,'添加商品失败');
    }

    /**
     * 修改购物车
     * @return array
     */
    public function actionUpdate()
    {
        $data = \Yii::$app->request->post();
        if (empty($data['shop_id'])) {
            WebBaseController::getAjaxData(2,'未传递shop_id');
        }
        if (empty($data['uid'])) {
            WebBaseController::getAjaxData(3,'未传递uid');
        }
        if (empty($data['product_id'])) {
            WebBaseController::getAjaxData(4,'未传递product_id');
        }
        if (empty($data['num'])) {
            WebBaseController::getAjaxData(5,'未传递num');
        }
        $ret = CartService::updateCart($data);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,'更新购物车成功');
        }
        WebBaseController::getAjaxData(6,'更新购物车失败');
    }

    /**
     * 清空购物车
     * @return array
     */
    public function actionEmpty()
    {
        $data = \Yii::$app->request->post();
        if (empty($data['shop_id'])) {
            WebBaseController::getAjaxData(2,'未传递shop_id');
        }
        if (empty($data['uid'])) {
            WebBaseController::getAjaxData(3,'未传递uid');
        }
        $ret = CartService::emptyCart($data['shop_id'],$data['uid']);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,'清空购物车成功');
        }
        WebBaseController::getAjaxData(4,'清空购物车失败');
    }

    /**
     * 删除购物车商品
     * @return array
     */
    public function actionDelete()
    {
        $data = \Yii::$app->request->post();
        if (empty($data['shop_id'])) {
            WebBaseController::getAjaxData(2,'未传递shop_id');
        }
        if (empty($data['uid'])) {
            WebBaseController::getAjaxData(3,'未传递uid');
        }
        if (empty($data['product_id'])) {
            WebBaseController::getAjaxData(4,'未传递product_id');
        }
        $ret = CartService::deleteProduct($data['shop_id'],$data['uid'],$data['product_id']);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,'删除购物车商品成功');
        }
        WebBaseController::getAjaxData(5,'删除购物车商品失败');
    }

    /**
     * 查看购物车
     * @return array
     */
    public function actionView()
    {
        $data = \Yii::$app->request->get();
        if (empty($data['shop_id'])) {
            WebBaseController::getAjaxData(2,'未传递shop_id');
        }
        if (empty($data['uid'])) {
            WebBaseController::getAjaxData(3,'未传递uid');
        }
        WebBaseController::getAjaxData(1,CartService::view($data['shop_id'],$data['uid']));
    }


}